package com.boyinet.demo.pipelineleakage.controller;

import cn.hutool.core.util.StrUtil;
import com.boyinet.demo.pipelineleakage.bean.primary.PipeLine;
import com.boyinet.demo.pipelineleakage.common.R;
import com.boyinet.demo.pipelineleakage.common.Tree;
import com.boyinet.demo.pipelineleakage.service.LeakageService;
import com.boyinet.demo.pipelineleakage.service.PipeLineService;
import com.boyinet.demo.pipelineleakage.vo.CalcKParams;
import com.boyinet.demo.pipelineleakage.vo.PipelineSearchParams;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @author lengchunyun
 */
@RestController
@RequestMapping("pipeline")
public class PipeLineController {

    private final PipeLineService pipeLineService;

    private final LeakageService leakageService;

    public PipeLineController(PipeLineService pipeLineService, LeakageService leakageService) {
        this.pipeLineService = pipeLineService;
        this.leakageService = leakageService;
    }

    @PostMapping("insert")
    R<Map<String, Void>> insert(PipeLine pipeLine) {
        if (StrUtil.isNotBlank(pipeLine.getXlsFileName())) {
            pipeLineService.save(pipeLine);
        } else {
            return R.nok();
        }
        return R.ok();
    }

    @GetMapping("list")
    R<List<PipeLine>> list(PipelineSearchParams params) {
        Page<PipeLine> pipeLinePage = pipeLineService.list(params);
        R<List<PipeLine>> ok = R.ok();
        ok.setData(pipeLinePage.getContent());
        ok.setTotal(pipeLinePage.getTotalElements());
        ok.setPage(pipeLinePage.getPageable());
        return ok;
    }

    @PostMapping("update")
    R<Map<String, Void>> update(PipeLine pipeLine) {
        pipeLineService.updateById(pipeLine);
        return R.ok();
    }

    @PostMapping("delete")
    R<Map<String, Void>> delete(Long id) {
        pipeLineService.deleteById(id);
        return R.ok();
    }

    @PostMapping("tree")
    R<List<Tree>> tree() {
        return R.ok(pipeLineService.tree());
    }

    @GetMapping("load")
    public R<PipeLine> load(Long id) {
        PipeLine pipeLine = pipeLineService.load(id);
        return R.ok(pipeLine);
    }

    @GetMapping("loadLeakage")
    public R<Void> loadLeakage(Long id, String time) {
        leakageService.load(id, time);
        return R.ok();
    }

    @GetMapping("listId")
    public R<List<PipeLine>> listId() {
        List<PipeLine> all = pipeLineService.findAll();
        return R.ok(all);
    }

    @GetMapping("listPipelineMap")
    public R<Map<String, Map<String, String>>> listPipelineMap() {
        return pipeLineService.listPipelineMap();
    }

    @GetMapping("listSensorMap")
    public R<Map<String, String>> listSensorMap(Long pipelineId) {
        Map<String, String> data = pipeLineService.listSensorMap(pipelineId);
        R<Map<String, String>> ok = R.ok(data);
        ok.setTotal(data.size());
        return ok;
    }


   @PostMapping("calcK")
    public R<String> calcK(CalcKParams params) {
        leakageService.calcK(params);
        return R.ok("计算完成");
    }
}
